const redis = require('redis');
// const session = require('express-session');
// let RedisStore  = require('connect-redis')(session);
let Client = redis.createClient($config._rc.port, $config._rc.host, $config._rc.opts);

// ERR operation not permitted 需检查reis.conf密码

Client.on('connect', function() {
    Client.set('author', 'luban', redis.print)
    Client.get('author', redis.print)
    console.log('connect');
})

Client.on('ready', function() {
    console.log('ready');
})

Client.on('end', function() {
    console.log('redis 断开');
})

global.$client = {
    _put: (k, v) => {
        Client.set($config._sc.name + k, v);
        Client.expire($config._sc.name + k, $config._sc.cookie.maxAge);
    },
    _get: k => {
        return new Promise((resolve, reject) => {
            Client.get($config._sc.name + k, (err, data) => {
                if(err) {
                    resolve(false)
                }
                Client.expire($config._sc.name + k, $config._sc.cookie.maxAge);
                resolve(JSON.parse(data))
            });
        })
    },
    _remove: k => {
        Client.del($config._sc.name + k);
    }
}
// module.exports = app => app.use(session( Object.assign( $config._sc, { store: new RedisStore({ client }) })));
